package net.alpcontrol.bi.colligation.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import net.alpcontrol.bi.colligation.service.IPjProjectTargetService;
import net.alpcontrol.bi.colligation.entity.PjProjectTarget;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alpcontrol.bi.bootstrap.bean.Message;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丛庆
 * @since 2020-09-10
 */
@CrossOrigin
@Api(tags = {""})
@RestController
@RequestMapping("/pjProjectTarget")
public class PjProjectTargetController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IPjProjectTargetService pjProjectTargetService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Message<IPage<PjProjectTarget>> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        Page<PjProjectTarget> page = new Page<>(pageNum, pageSize);
        IPage<PjProjectTarget> result = pjProjectTargetService.selectPage(page);
        return Message.success("查询成功",result);
    }


    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询数据")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Message<PjProjectTarget> getById(@RequestParam("id") String id){
        return Message.success("查询成功",pjProjectTargetService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message add(@RequestBody PjProjectTarget pjProjectTarget){
        pjProjectTargetService.save(pjProjectTarget);
        return Message.success("新增成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public Message delete(@RequestParam("ids") List<String> ids){
        pjProjectTargetService.removeByIds(ids);
        return Message.success("删除成功");
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Message update(@RequestBody PjProjectTarget pjProjectTarget){
        pjProjectTargetService.saveOrUpdate(pjProjectTarget);
        return Message.success("修改成功");
    }

    @ApiOperation(value = "根据年份和公司查询项目的目标值")
    @GetMapping(value = "getPjTargetByYearAndCo")
    public Message<List> getPjTargetByYearAndCo(@RequestParam("year") String year,
                                                @RequestParam("companyId") String companyId){
        List<Map> result = pjProjectTargetService.getPjTargetByYearAndCo(year,companyId);
        return Message.success("0","查询成功",result);
    }
    /**
     * 导入预算值
     * @param multipartFile
     * @return
     */
    @ApiOperation(value = "导入目标值")
    @RequestMapping(value = "/importTargets", method = RequestMethod.POST)
    public Message importBudgets(@RequestParam("file") MultipartFile multipartFile,
                                 @RequestParam("year") String year,
                                 @RequestParam("companyId") String companyId) throws Exception{

        pjProjectTargetService.importTargets(multipartFile,year,companyId);
        return Message.success("导入成功");
    }
    /**
     * 根据年份和公司导出预算数据
     * @param year
     * @return
     */
    @ApiOperation(value = "导出预算数据")
    @RequestMapping(value = "/exportTarget", method = RequestMethod.GET)
    public void exportBudget(HttpServletResponse servletResponse, @RequestParam("year") String year, @RequestParam("companyId")String companyId){
        pjProjectTargetService.exportTarget(servletResponse,year,companyId);
    }
}
